﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.TeamFoundation.VersionControl.Client;
using System.Windows.Forms;

namespace TFSPlus.Commands
{
	public class NewPendingChangeCheck : IAddinUICommand
	{

        #region IAddinUICommand Members

        public void UISetup(EnvDTE80.DTE2 applicationObject, EnvDTE.AddIn addInInstance, EnvDTE80.Commands2 commands, object[] contextGUIDS)
        {
            
        }

        public void Startup(EnvDTE80.DTE2 applicationObject, EnvDTE.AddIn addInInstance)
        {
            if (Connect.VersionControlExt.Explorer.Workspace != null)
            {
                Connect.VersionControlExt.Explorer.Workspace.VersionControlServer.OperationStarting += VersionControlServer_OperationStarting;
                Connect.VersionControlExt.Explorer.Workspace.VersionControlServer.NewPendingChange += VersionControlServer_NewPendingChange;
                Connect.VersionControlExt.Explorer.Workspace.VersionControlServer.OperationFinished += VersionControlServer_OperationFinished;
            }
        }


        static bool haveShownNonLatestWarning = false;
        static void VersionControlServer_OperationFinished(object sender, OperationEventArgs e)
        {
        }

        static void VersionControlServer_OperationStarting(object sender, OperationEventArgs e)
        {
            haveShownNonLatestWarning = false;
        }

        static void VersionControlServer_NewPendingChange(object sender, PendingChangeEventArgs e)
        {
            if (!e.IsLatest && !e.PendingChange.IsBranch)
            {
                if (Connect.VersionControlExt.Explorer.Workspace != null)
                {
                    if (!haveShownNonLatestWarning)
                    {
                        haveShownNonLatestWarning = true;
                        MessageBox.Show("You have checked out a file while you do not have the latest code. You may have to merge changes when you checkin.");
                    }
                }
            }
        }

        public EnvDTE.vsCommandStatus QueryStatus()
        {
            return EnvDTE.vsCommandStatus.vsCommandStatusEnabled;
        }

        public void Exec()
        {
        }

        public void Disconnect(EnvDTE80.DTE2 applicationObject, EnvDTE.AddIn addInInstance)
        {
        }

        #endregion
    }
}
